package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.domain.cargo.ShippingExample;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.utils.DownloadUtil;
import cn.itcast.utils.UUIDUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("cargo/shipping")
public class ShippingController extends BaseController {
    @Reference
    ShippingService shippingService;
    @Reference
    PackingService packingService;

    @RequestMapping(value = "list", name = "委托管理")
    public String findAll(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        ShippingExample shippingExample = new ShippingExample();
        ShippingExample.Criteria criteria = shippingExample.createCriteria();
        criteria.andNotifyPartyEqualTo("Jacky");
        PageInfo<Shipping> pageInfo = shippingService.findAll(page, size, shippingExample);
        request.setAttribute("page", pageInfo);
        return "cargo/shipping/shipping-list";
    }

    @RequestMapping(value = "toAdd", name = "跳转到添加页面")
    public String toAdd(String id) {
        request.setAttribute("packingId",id);
        return "cargo/shipping/shipping-add";
    }

    @RequestMapping(value = "edit", name = "新增委托单")
    public String edit(Shipping shipping) {
        String packingId = (String) request.getAttribute("packingId");
        Packing packing = new Packing();
        packing.setId(packingId);
        packing.setState("1");
        if (packingId!=""&&packingId!=null){
            shipping.setShippingOrderId(packingId);
        }else {
            shipping.setShippingOrderId(UUIDUtils.getId());
        }

        shipping.setCheckBy(getLoginUser().getUserName());
        shipping.setCreateTime(new Date());
        shipping.setCreateDept(getLoginUser().getDeptName());
        shipping.setState(0);
        shippingService.insertSelective(shipping);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "delete", name = "删除")
    public String delete(String id) {
        shippingService.deleteByPrimaryKey(id);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "outExcel", name = "导出Excel表")
    public void outExcel(String id, HttpServletResponse response) throws IOException {
        Shipping shipping = shippingService.selectByPrimaryKey(id);

        Workbook wk = new XSSFWorkbook();
        Sheet sheet = wk.createSheet();
        //第一行
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(1);
        cell.setCellValue("编号");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getShippingOrderId());
        //第二行
        row = sheet.createRow(1);
        cell = row.createCell(1);
        cell.setCellValue("运输方式");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getOrderType());

        row = sheet.createRow(2);
        cell = row.createCell(1);
        cell.setCellValue("货主");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getShipper());

        row = sheet.createRow(3);
        cell = row.createCell(1);
        cell.setCellValue("货单抬头");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getConsignee());

        row = sheet.createRow(4);
        cell = row.createCell(1);
        cell.setCellValue("正式通知人");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getNotifyParty());

        row = sheet.createRow(5);
        cell = row.createCell(1);
        cell.setCellValue("使用证");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getLcNo());

        row = sheet.createRow(6);
        cell = row.createCell(1);
        cell.setCellValue("装运港");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getPortOfLoading());

        row = sheet.createRow(7);
        cell = row.createCell(1);
        cell.setCellValue("转运港");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getPortOfTrans());

        row = sheet.createRow(8);
        cell = row.createCell(1);
        cell.setCellValue("装运港");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getPortOfDischarge());

        row = sheet.createRow(9);
        cell = row.createCell(1);
        cell.setCellValue("是否分批");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getIsBatch());

        row = sheet.createRow(10);
        cell = row.createCell(1);
        cell.setCellValue("扼要说明");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getRemark());

        /*row = sheet.createRow(11);
        cell = row.createCell(1);
        cell.setCellValue("状态");
        cell = row.createCell(2);
        cell.setCellValue(shipping.getState());*/

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        wk.write(bos);

        DownloadUtil.download(bos,response,"委托单.xlsx");

    }
}